//
//  AppDelegate.swift
//  weibopush
//
//  Created by 龚欣 on 15/6/24.
//  Copyright © 2015年 gong. All rights reserved.
//

import UIKit
import AFNetworking
     var sharedAccount = UserAccount.loadData()

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    let SwitchRootViewControllerNotification = "SwitchRootViewControllerNotification"

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        SQLiteManager.sharedManager.openDB("status.db")
        
        NSURLCache.setSharedURLCache(NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil))
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        
        
        
        setUpUI()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultController()
        window?.makeKeyAndVisible()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchViewController:", name: SwitchRootViewControllerNotification, object: nil)
        
        return true
    }

    func switchViewController(note : NSNotification){
        let isLogin = note.object as! Bool
        if isLogin{
            self.window?.rootViewController = MainViewController()
            return
        }
        
        self.window?.rootViewController = WelcomeViewController()
        
    
    }
    
    func isNewVersion() -> Bool{
    
        let versionKey = "versionKey"
        //从沙盒中取出版本号
        let lastVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        //当前程序版本号
        let currentVersionString = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let currentVersion = Double(currentVersionString)
       
        //把新版本存入到沙盒
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion!, forKey: versionKey)
        
       
        return currentVersion > lastVersion
        
    }
    
    func defaultController() -> UIViewController{
    
        if sharedAccount == nil {
            return MainViewController()
        }
        
        if isNewVersion(){
            return NewFetureCollectionViewController()
        }
    
        return WelcomeViewController()
    
    }
    
    func setUpUI(){
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

}

